package huzh.foundation.pool.connectionpool;

import huzh.foundation.util.SysUtil;

import java.sql.Connection;

/**
 *<p>title:MonitorConnectionStatus</p>
 *<p>description:</p>
 * @since 2014-6-7
 * @author huzh
 * @version 1.0
 */
public class MonitorConnectionStatus {
    /**id*/
    private String id;
    /**connection*/
    private Connection connection;
    /**createDate*/
    private String createDate;
    /**getTime*/
    private long getTime;
    /**running*/
    private boolean running;
    /**userThread*/
    private Thread userThread;
    /**userDetail*/
    private String userDetail;

    /**
     * @param connection
     */
    public MonitorConnectionStatus(Connection connection){
        this.id = ("" + connection.hashCode());
        this.connection = connection;
        this.createDate = SysUtil.getSysDate24();
        this.running = false;
        this.getTime = 0L;
        this.userThread = null;
        this.userDetail = "";
    }

    /**
     * @param running
     */
    public void setRunning(boolean running) {
        this.running = running;
        if (this.running) {
            this.getTime = System.currentTimeMillis();
            this.userThread = Thread.currentThread();
            this.userDetail = SysUtil.getCurrentThreadStackTrace();
        } else {
            this.getTime = 0L;
            this.userThread = null;
            this.userDetail = "";
        }
    }

    /**
     * @return
     */
    public Connection getConnection() {
        return this.connection;
    }

    /**
     * @return running
     */
    public boolean isRunning() {
        return this.running;
    }

    /**
     * @return userThread
     */
    public Thread getUserThread() {
        return this.userThread;
    }

    /**
     * @return userDetail
     */
    public String getUserDetail() {
        return this.userDetail;
    }

    /**
     * @return createDate
     */ 
    public String getCreateDate() {
        return this.createDate;
    }

    /**
     * @return id
     */
    public String getId() {
        return this.id;
    }

    /**
     * @return
     */
    public long getRunningTime() {
        if (isRunning()) {
            return System.currentTimeMillis() - this.getTime;
        }
        return 0L;
    }

    /**
     * @return
     */
    public String toString(){
        return "\r\n[id:" + this.id + "\r\nconnection:" + this.connection + "\r\ncreateDate:" + this.createDate 
            + "\r\nrunning:" + this.running + "\r\ngetRunningTime:" + getRunningTime() + "\r\nuserThread:" 
            + this.userThread + "\r\nuserDetail:" + this.userDetail + "]\r\n";
    }
}
